package com.niit.blog.service.impl;

import com.niit.blog.entity.Dto.BlogEsDto;
import com.niit.blog.entity.Dto.EsBlogFields;
import com.niit.blog.entity.domain.Blog;
import com.niit.blog.es.EsIndexInfo;
import com.niit.blog.es.EsRestClient;
import com.niit.blog.es.EsSourceData;
import com.niit.blog.service.BlogEsService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BlogEsServiceImpl implements BlogEsService {
    @Override
    public List<Blog> getBlogByEs(Blog blog) {
        return null;
    }

    @Override
    public boolean insert(BlogEsDto blogEsDto) {
        EsSourceData esSourceData = new EsSourceData();
        //转换数据
        Map<String,Object> data = convertData(blogEsDto);
        esSourceData.setData(data);
        esSourceData.setDocId(blogEsDto.getDocId().toString());
        return EsRestClient.insertDoc(getESIndexInfo(),esSourceData);
    }
   //数据转换
    private Map<String, Object> convertData(BlogEsDto blogEsDto) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put(EsBlogFields.BLOG_ID,blogEsDto.getBlogId());
        objectObjectHashMap.put(EsBlogFields.DOC_ID,blogEsDto.getDocId());
        objectObjectHashMap.put(EsBlogFields.BLOG_TAGS,blogEsDto.getBlogTags());
        objectObjectHashMap.put(EsBlogFields.BLOG_CATAGORYID,blogEsDto.getBlogCategoryId());
        objectObjectHashMap.put(EsBlogFields.BLOG_CATEGORYNAME,blogEsDto.getBlogCategoryName());
        objectObjectHashMap.put(EsBlogFields.BLOG_CONTENT,blogEsDto.getBlogContent());
        objectObjectHashMap.put(EsBlogFields.CREATE_USER,blogEsDto.getCreateUser());
        objectObjectHashMap.put(EsBlogFields.BLOG_TITLE,blogEsDto.getBlogTitle());
        return objectObjectHashMap;

    }
 //拿到Es的索引信息
    private EsIndexInfo getESIndexInfo() {
        EsIndexInfo esIndexInfo = new EsIndexInfo();
        esIndexInfo.setIndexName("blog_index");
        esIndexInfo.setClusterName("4a0d2165817c");
        return esIndexInfo;
    }




}
